<?php
// 删除医生信息
// header("content-type:text/html;charset=UTF-8");
include_once("conn.php");

$docid = $_GET["docID"];

$select = "SELECT * FROM order_infos WHERE doc_ID = '" . $docid . "'";
$result = mysqli_query($connect_DB, $select);

if (mysqli_num_rows($result) > 0) {
    die("<script>alert('删除失败！该医生有未处理的预约信息！');history.go(-1);</script>");
} else {
    $selectdoc = "SELECT * FROM doc_infos WHERE doc_ID = '" . $docid . "'";
    $resultdoc = mysqli_query($connect_DB, $selectdoc);
    if(!$resultdoc){
        die("<script>alert('删除失败！查询信息失败！');history.go(-1);</script>");
    }
    $row = mysqli_fetch_object($resultdoc);
    //设置中国时区
    date_default_timezone_set('Asia/Shanghai');
    $insert = "INSERT INTO history_doc_infos SET doc_ID = '" . $docid . "', doc_Name = '" . $row->doc_Name . "', del_Date = '" . date("Y-m-d H:i:s") . "'";
    if(!mysqli_query($connect_DB, $insert)){
        die("<script>alert('删除失败！请重试！');history.go(-1);</script>");
    }
    
}


$del = "DELETE FROM doc_infos WHERE doc_ID = '" . $docid . "'";

$file_path = "../public/pic/doc_imgs/" . $row->doc_PhotoAddress;
if (file_exists($file_path)) {
    unlink($file_path); //删除图片文件夹中的图片文件
}

if (!mysqli_query($connect_DB, $del)) {
    echo "<script>alert('删除信息失败！请在数据库端进行清理！');history.go(-1);</script>";
} else {
    echo "<script>alert('删除成功！');location='admin_SearchDoctorInfo.php';</script>";
}

$connect_DB->close();
?>